// RAIIHelpers.hh
// RAII辅助类，用于异常安全的资源管理
// 合并了ThreadSafeUtils的功能

#ifndef MEDIA_UTIL_RAII_HELPERS_HH
#define MEDIA_UTIL_RAII_HELPERS_HH

#include <memory>
#include <pthread.h>
#include <stdio.h>

// ThreadSafeUtils命名空间（保持向后兼容）
namespace ThreadSafeUtils {
    class ScopedLock {
    public:
        explicit ScopedLock(pthread_mutex_t& mutex) : m_mutex(mutex) { 
            pthread_mutex_lock(&m_mutex); 
        }
        ~ScopedLock() { 
            pthread_mutex_unlock(&m_mutex); 
        }
    private:
        pthread_mutex_t& m_mutex;
        ScopedLock(const ScopedLock&) = delete;
        ScopedLock& operator=(const ScopedLock&) = delete;
    };

    class ConditionVariable {
    public:
        ConditionVariable() { 
            pthread_cond_init(&m_cond, NULL); 
        }
        ~ConditionVariable() { 
            pthread_cond_destroy(&m_cond); 
        }
        pthread_cond_t* nativeHandle() { 
            return &m_cond; 
        }
    private:
        pthread_cond_t m_cond;
        ConditionVariable(const ConditionVariable&) = delete;
        ConditionVariable& operator=(const ConditionVariable&) = delete;
    };
}

// RAII互斥锁包装器
class ScopedMutexLock {
public:
    explicit ScopedMutexLock(pthread_mutex_t& mutex) : m_mutex(mutex) {
        pthread_mutex_lock(&m_mutex);
    }
    
    ~ScopedMutexLock() {
        pthread_mutex_unlock(&m_mutex);
    }
    
    // 禁止拷贝和移动
    ScopedMutexLock(const ScopedMutexLock&) = delete;
    ScopedMutexLock& operator=(const ScopedMutexLock&) = delete;
    
private:
    pthread_mutex_t& m_mutex;
};

// RAII文件指针包装器
class ScopedFILE {
public:
    explicit ScopedFILE(FILE* file) : m_file(file) {}
    
    ~ScopedFILE() {
        if (m_file != nullptr) {
            fclose(m_file);
        }
    }
    
    // 获取原始指针
    FILE* get() const { return m_file; }
    
    // 释放所有权
    FILE* release() {
        FILE* tmp = m_file;
        m_file = nullptr;
        return tmp;
    }
    
    // 禁止拷贝，允许移动
    ScopedFILE(const ScopedFILE&) = delete;
    ScopedFILE& operator=(const ScopedFILE&) = delete;
    ScopedFILE(ScopedFILE&& other) noexcept : m_file(other.m_file) {
        other.m_file = nullptr;
    }
    ScopedFILE& operator=(ScopedFILE&& other) noexcept {
        if (this != &other) {
            if (m_file != nullptr) {
                fclose(m_file);
            }
            m_file = other.m_file;
            other.m_file = nullptr;
        }
        return *this;
    }
    
    // 操作符重载
    operator bool() const { return m_file != nullptr; }
    FILE* operator->() const { return m_file; }
    
private:
    FILE* m_file;
};

// RAII资源清理器（通用）
template<typename T>
class ScopedResource {
public:
    explicit ScopedResource(T* resource, void (*deleter)(T*)) 
        : m_resource(resource), m_deleter(deleter) {}
    
    ~ScopedResource() {
        if (m_resource != nullptr && m_deleter != nullptr) {
            m_deleter(m_resource);
        }
    }
    
    // 获取原始指针
    T* get() const { return m_resource; }
    
    // 释放所有权
    T* release() {
        T* tmp = m_resource;
        m_resource = nullptr;
        return tmp;
    }
    
    // 禁止拷贝，允许移动
    ScopedResource(const ScopedResource&) = delete;
    ScopedResource& operator=(const ScopedResource&) = delete;
    ScopedResource(ScopedResource&& other) noexcept 
        : m_resource(other.m_resource), m_deleter(other.m_deleter) {
        other.m_resource = nullptr;
        other.m_deleter = nullptr;
    }
    ScopedResource& operator=(ScopedResource&& other) noexcept {
        if (this != &other) {
            if (m_resource != nullptr && m_deleter != nullptr) {
                m_deleter(m_resource);
            }
            m_resource = other.m_resource;
            m_deleter = other.m_deleter;
            other.m_resource = nullptr;
            other.m_deleter = nullptr;
        }
        return *this;
    }
    
    // 操作符重载
    operator bool() const { return m_resource != nullptr; }
    T* operator->() const { return m_resource; }
    T& operator*() const { return *m_resource; }
    
private:
    T* m_resource;
    void (*m_deleter)(T*);
};

#endif // MEDIA_UTIL_RAII_HELPERS_HH

